#!/bin/bash
# +-------------------------------------------------------------------
# | HC-docker一键安装程序
# | Author: 王天良(hecdemo@winqi.cn)
# +--------------------------------------------------------------------
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
install_tmp='/tmp/install.pl'
sys_path="/hcdemo"


Install_Docker_ce()

{
	#install docker-ce
	#pip install pytz
	yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-selinux docker-engine-selinux docker-engine -y
	yum install -y yum-utils device-mapper-persistent-data lvm2
	yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
	yum makecache fast
	yum -y install docker-ce
	


	#systemctl or service
	if [ -f /usr/bin/systemctl ];then
		systemctl stop getty@tty1.service
		systemctl mask getty@tty1.service
		systemctl enable docker
		systemctl start docker
	else
		chkconfig --add docker
		chkconfig --level 2345 docker on
		service docker start
	fi
}


Install_docker()
{
	mkdir -p /$sys_path/server/docker
	systemctl start docker
	is_start=$(systemctl status docker|grep "active (running)")
	if [ "$is_start" == "" ];then
		rm -f /var/lib/docker
		if [ -f "/usr/bin/apt-get" ];then
	    	Install_Ubuntu_ce
	    elif [ -f "/usr/bin/yum" ];then
	    	Install_Docker_ce
	    fi
	fi
	echo '安装完成' > $install_tmp
}


Uninstall_docker()
{
	rm -rf /$sys_path/server/docker
#	rm -rf /usr/bin/docker 
	if [ -f "/usr/bin/apt-get" ];then
	    systemctl stop docker
    	#pip uninstall docker -y
		yum -y remove docker docker-common docker-selinux docker-engine

    elif [ -f "/usr/bin/yum" ];then
    	if [ -f /usr/bin/systemctl ];then
			systemctl disable docker
			systemctl stop docker
		else
			service docker stop
			chkconfig --level 2345 docker off
			chkconfig --del docker
		fi
		#pip uninstall docker -y
		yum -y remove docker docker-common docker-selinux docker-engine
    fi

}

action=$1
if [ "${1}" == 'install' ];then
	Install_docker
elif  [ "${1}" == 'update' ];then
	Install_docker
elif [ "${1}" == 'uninstall' ];then
	Uninstall_docker
fi

